const Mock = require('mockjs')

var data = Mock.mock({
  items: [
    {
      id: '0',
      name: '张三',
      sex: 'male',
      account: '20200504110',
      birthday: '@date'
    },
    {
      id: '1',
      name: '李四',
      sex: 'male',
      account: '20200504110',
      birthday: '@date'
    },
    {
      id: '2',
      name: '王五',
      sex: 'male',
      account: '20200504110',
      birthday: '@date'
    },
    {
      id: '3',
      name: '赵六',
      sex: 'male',
      account: '20200504110',
      birthday: '@date'
    },
    {
      id: '4',
      name: '翠花',
      sex: 'female',
      account: '20200504110',
      birthday: '@date'
    },
    {
      id: '5',
      name: '二狗',
      sex: 'male',
      account: '20200504110',
      birthday: '@date'
    },
    {
      id: '6',
      name: '老六',
      sex: 'female',
      account: '123',
      birthday: '@date'
    }
  ]
})

module.exports = [
  {
    url: '/vue-admin-template/user/list',
    type: 'get',
    response: (config) => {
      const params = config.query
      let datas = []
      if (params.name === '' && params.account === '') {
        datas = data.items
      } else {
        datas = data.items.filter(
          (item) => params.name == item.name || params.account == item.account
        )
        //   for (let index = 0; index < data.items.length; index++) {
        //     if (
        //       params.name == data.items[index].name ||
        //       params.account == data.items[index].account
        //     ) {
        //       datas.push(data.items[index]);
        //     }
        //   }
      }
      return {
        code: 20000,
        data: {
          total: datas.length,
          items: datas
        }
      }
    }
  },

  {
    url: '/vue-admin-template/user/update',
    type: 'post',
    response: (config) => {
      const body = config.body
      // for (let index = 0; index < data.items.length; index++) {
      //   if (body.id === data.items[index].id) {
      //     data.items[index] = body;
      //   }
      // }
      const index = data.items.findIndex((r) => r.id === body.id)
      data.items.splice(index, 1, body)
      return {
        code: 20000,
        msg: '成功'
      }
    }
  },

  {
    url: '/vue-admin-template/user/delete',
    type: 'post',
    response: (config) => {
      const body = config.body
      // for (let index = 0; index < data.items.length; index++) {
      //   if (body.id === data.items[index].id) {
      //     data.items.splice(index, 1);
      //   }
      // }
      const index = data.items.findIndex((r) => r.id === body.id)
      data.items.splice(index, 1)
      return {
        code: 20000,
        msg: '成功'
      }
    }
  }
]
